<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax_books extends CI_Controller {
    
    function Ajax_books()
    {
        parent::__construct();
    }
    
    function grid()
    {
        // Load necessary library, helper and model files
        $this->load->model('book_model');
        $this->load->library('flexigrid');

        // List of all fields that can be sortable. This is Optional.
        // This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
        $valid_fields = array('id', 'date', 'class', 'author', 'title', 'source', 'cost', 'publisher', 'year', 'remarks');
        $this->flexigrid->validate_post('id', 'desc', $valid_fields);

        // Get agent data
        $records = $this->book_model->grid();

        $record_items = array();
        foreach($records['records']->result() as $row)
        {
            // Build flexigrid row array
            $record_items[] = array(
                $row->id,
                $row->id,
                $row->date,
                $row->class,
                $row->author,
                $row->title,
                $row->edition,
                $row->volumes,
                $row->pages,
                $row->source,
                $row->cost,
                $row->publisher,
                $row->year,
                $row->remarks
            );
        }

        // Print
        $this->output->set_header($this->config->item('json_header'));
        $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
    }
    
}

/* End of file ajax_books.php */
/* Location: ./application/controllers/ajax_books.php */